//
//  MBProgressHUD+LCHud.m
//  randomNum
//
//  Created by ChungRowe on 2021/8/11.
//

#import "MBProgressHUD+LCHud.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import "UIView+MHView.h"

#define hideHUDTime (30)

@implementation MBProgressHUD (LCHud)

+ (MBProgressHUD*)createMBProgressHUDviewWithMessage:(NSString*)message isWindiw:(BOOL)isWindow {
    // 不传位置，默认居中
    return [self createMBProgressHUDviewWithMessage:message isWindiw:isWindow pointType:(ShowHUDPointType_Center)];
}

+ (MBProgressHUD*)createMBProgressHUDviewWithMessage:(NSString*)message isWindiw:(BOOL)isWindow pointType:(ShowHUDPointType)pointType {
    
    UIView  *view = isWindow ? [UIApplication sharedApplication].keyWindow : [self getCurrentUIVC].view;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = message ? message : @"";
    
    hud.label.font = [UIFont systemFontOfSize:15];
    
    // 内容颜色
    hud.tintColor = [UIColor whiteColor];
    hud.contentColor = [UIColor whiteColor];
    
    // 背景框样式颜色
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
//    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.bezelView.backgroundColor = [MHTheme getMHMainTextColor];
    
    // 当显示和隐藏HUD时应该使用的动画类型。
    hud.animationType = MBProgressHUDAnimationFade;
    
    // 当HUD被隐藏时，从其父视图中移除它。默认为没有。
    hud.removeFromSuperViewOnHide = YES;
    
    // hud
    if (pointType == ShowHUDPointType_Center) {
        hud.offset = CGPointMake(0.f, 0.f);
    } else if (pointType == ShowHUDPointType_Bottom) {
        hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
    }
    
    return hud;
}

#pragma mark-------------------- show Activity----------------------------
+ (void)showActivityMessageInWindow:(NSString*)message {
    [self showActivityMessage:message isWindow:true timer:0];
}
+ (void)showActivityMessageInView:(NSString*)message {
    [self showActivityMessage:message isWindow:false timer:0];
}
+ (void)showActivityMessageInWindow:(NSString*)message timer:(int)aTimer {
    [self showActivityMessage:message isWindow:true timer:aTimer];
}
+ (void)showActivityMessageInView:(NSString*)message timer:(int)aTimer {
    [self showActivityMessage:message isWindow:false timer:aTimer];
}
+ (void)showActivityMessageInWindow:(NSString*)message timer:(int)aTimer pointType:(ShowHUDPointType)pointType {
    [self showActivityMessage:message isWindow:true timer:aTimer pointType:pointType];
}
+ (void)showActivityMessageInView:(NSString*)message timer:(int)aTimer pointType:(ShowHUDPointType)pointType {
    [self showActivityMessage:message isWindow:false timer:aTimer pointType:pointType];
}

+ (void)showActivityMessage:(NSString*)message isWindow:(BOOL)isWindow timer:(int)aTimer {
    
    MBProgressHUD *hud = [self createMBProgressHUDviewWithMessage:message isWindiw:isWindow];
    hud.mode = MBProgressHUDModeIndeterminate;
    [self hideHUD:hud timer:aTimer];
}
+ (void)showActivityMessage:(NSString*)message isWindow:(BOOL)isWindow timer:(int)aTimer pointType:(ShowHUDPointType)pointType {
    
    MBProgressHUD *hud = [self createMBProgressHUDviewWithMessage:message isWindiw:isWindow pointType:pointType];
    hud.mode = MBProgressHUDModeIndeterminate;
    [self hideHUD:hud timer:aTimer];
}

#pragma mark-------------------- show Tip----------------------------
+ (void)showTipMessageInWindow:(NSString*)message {
    [self showTipMessage:message isWindow:true timer:0];
}
+ (void)showTipMessageInView:(NSString*)message {
    [self showTipMessage:message isWindow:false timer:0];
}
+ (void)showTipMessageInWindow:(NSString*)message timer:(int)aTimer {
    [self showTipMessage:message isWindow:true timer:aTimer];
}
+ (void)showTipMessageInView:(NSString*)message timer:(int)aTimer {
    [self showTipMessage:message isWindow:false timer:aTimer];
}
+ (void)showTipMessageInWindow:(NSString*)message timer:(int)aTimer pointType:(ShowHUDPointType)pointType {
    [self showTipMessage:message isWindow:true timer:aTimer pointType:pointType];
}
+ (void)showTipMessageInView:(NSString*)message timer:(int)aTimer pointType:(ShowHUDPointType)pointType {
    [self showTipMessage:message isWindow:false timer:aTimer pointType:pointType];
}

+ (void)showTipMessage:(NSString*)message isWindow:(BOOL)isWindow timer:(int)aTimer {
    
    MBProgressHUD *hud = [self createMBProgressHUDviewWithMessage:message isWindiw:isWindow];
    hud.mode = MBProgressHUDModeText;
    [self hideHUD:hud timer:aTimer];
}
+ (void)showTipMessage:(NSString*)message isWindow:(BOOL)isWindow timer:(int)aTimer pointType:(ShowHUDPointType)pointType {
    
    MBProgressHUD *hud = [self createMBProgressHUDviewWithMessage:message isWindiw:isWindow pointType:pointType];
    hud.mode = MBProgressHUDModeText;
    [self hideHUD:hud timer:aTimer];
}

#pragma mark-------------------- show Image ----------------------------
+ (void)showSuccessMessage:(NSString *)Message {
    NSString *name = @"MBProgressHUD+LCHud.bundle/MBHUD_Success";
    [self showCustomIconInWindow:name message:Message];
}
+ (void)showErrorMessage:(NSString *)Message {
    NSString *name = @"MBProgressHUD+LCHud.bundle/MBHUD_Error";
    [self showCustomIconInWindow:name message:Message];
}
+ (void)showInfoMessage:(NSString *)Message {
    NSString *name = @"MBProgressHUD+LCHud.bundle/MBHUD_Info";
    [self showCustomIconInWindow:name message:Message];
}
+ (void)showWarnMessage:(NSString *)Message {
    NSString *name =@"MBProgressHUD+LCHud.bundle/MBHUD_Warn";
    [self showCustomIconInWindow:name message:Message];
}
+ (void)showCustomIconInWindow:(NSString *)iconName message:(NSString *)message {
    [self showCustomIcon:iconName message:message isWindow:true timer:2];
}
+ (void)showCustomIconInView:(NSString *)iconName message:(NSString *)message {
    [self showCustomIcon:iconName message:message isWindow:false timer:2];
}
+ (void)showCustomIconInWindow:(NSString *)iconName message:(NSString *)message timer:(int)aTimer {
    [self showCustomIcon:iconName message:message isWindow:false timer:aTimer];
}
+ (void)showCustomIconInView:(NSString *)iconName message:(NSString *)message timer:(int)aTimer {
    [self showCustomIcon:iconName message:message isWindow:false timer:aTimer];
}

+ (void)showCustomIcon:(NSString *)iconName message:(NSString *)message isWindow:(BOOL)isWindow timer:(int)aTimer {
    
    MBProgressHUD *hud  =  [self createMBProgressHUDviewWithMessage:message isWindiw:isWindow];
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
    hud.mode = MBProgressHUDModeCustomView;
    [self hideHUD:hud timer:aTimer];
    
}

#pragma mark-------------------- show Other Mode----------------------------
+ (void)showHudWithsHUDMode:(MBProgressHUDMode)mode message:(nonnull NSString *)message inWindow:(BOOL)inWindow timer:(int)aTimer {
    
    MBProgressHUD *hud = [self createMBProgressHUDviewWithMessage:message isWindiw:NO];
    hud.mode = mode;
    
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
        // Do something useful in the background and update the HUD periodically.
        [self doSomeWorkWithProgress:hud timer:aTimer];
        dispatch_async(dispatch_get_main_queue(), ^{
            [hud hideAnimated:YES];
        });
    });

//    [self hideHUD:hud timer:aTimer];
}

+ (void)showHudWithsHUDMode:(MBProgressHUDMode)mode message:(NSString *)message inWindow:(BOOL)inWindow timer:(int)aTimer canCancel:(BOOL)canCancel {
    
    MBProgressHUD *hud = [self createMBProgressHUDviewWithMessage:message isWindiw:NO];
    hud.mode = mode;
    
    NSProgress *progressObject = [NSProgress progressWithTotalUnitCount:100];
    hud.progressObject = progressObject;
    
    [hud.button setTitle:NSLocalizedString(@"Cancel", @"HUD cancel button title") forState:UIControlStateNormal];
    [hud.button addTarget:progressObject action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];

    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
        // Do something useful in the background and update the HUD periodically.
        [self doSomeWorkWithProgressObject:progressObject timer:aTimer];
        dispatch_async(dispatch_get_main_queue(), ^{
            [hud hideAnimated:YES];
        });
    });
    
}

/// 进度
+ (void)doSomeWorkWithProgress:(MBProgressHUD *)hud timer:(int)aTimer {
    // This just increases the progress indicator in a loop.
    float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.01f;
        dispatch_async(dispatch_get_main_queue(), ^{
            // Instead we could have also passed a reference to the HUD
            // to the HUD to myProgressTask as a method parameter.
            hud.progress = progress;
        });
        usleep(aTimer * 10000);
    }
}

/// 进度对象
+ (void)doSomeWorkWithProgressObject:(NSProgress *)progressObject timer:(int)aTimer {
    // This just increases the progress indicator in a loop.
    while (progressObject.fractionCompleted < 1.0f) {
        if (progressObject.isCancelled) break;
        [progressObject becomeCurrentWithPendingUnitCount:1];
        [progressObject resignCurrent];
        usleep(aTimer * 10000);
    }
}

#pragma mark - hide
+ (void)hideHUD:(MBProgressHUD *)hud timer:(int)aTimer {
    
    if (aTimer > 0) {
        [hud hideAnimated:YES afterDelay:aTimer];
    }
    
    // 外部手动关闭：
    // [MBProgressHUD hideHUD];
    // 防止太久 hud aTimer = 0，默认添加15s关闭
    if (aTimer <= 0) {
        [hud hideAnimated:YES afterDelay:hideHUDTime];
    }
    
    
}
+ (void)hideHUD {
    
    UIView *winView = (UIView*)[UIApplication sharedApplication].keyWindow;
    
    [self hideHUDForView:winView animated:YES];
    [self hideHUDForView:[self getCurrentUIVC].view animated:YES];
}
//
//#pragma mark --- 获取当前Window试图---------
//// 获取当前屏幕显示的viewcontroller
//+ (UIViewController*)getCurrentWindowVC {
//
//    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
//
//    // app默认windowLevel是UIWindowLevelNormal，如果不是，找到它
//    if (window.windowLevel != UIWindowLevelNormal) {
//        NSArray *windows = [[UIApplication sharedApplication] windows];
//        for(UIWindow * tmpWin in windows) {
//            if (tmpWin.windowLevel == UIWindowLevelNormal) {
//                window = tmpWin;
//                break;
//            }
//        }
//    }
//
//    id nextResponder = nil;
//    UIViewController *appRootVC = window.rootViewController;
//
//    if ([appRootVC isKindOfClass:[UITabBarController class]]) {
//        nextResponder = appRootVC;
//    } else {
//        if (appRootVC.presentedViewController) {
//            // 1、通过present弹出VC，appRootVC.presentedViewController不为nil
//            nextResponder = appRootVC.presentedViewController;
//        } else {
//            if (appRootVC.childViewControllers > 0) {
//                nextResponder = appRootVC.childViewControllers[0];
//            } else {
//                //2、通过navigationcontroller弹出VC
//                UIView *frontView = [[window subviews] objectAtIndex:0];
//                nextResponder = [frontView nextResponder];
//            }
//        }
//    }
//
//    return nextResponder;
//}
//
//+ (UINavigationController*)getCurrentNaVC {
//
//    UIViewController *viewVC = (UIViewController*)[self getCurrentWindowVC];
//
//    UINavigationController *naVC;
//    if ([viewVC isKindOfClass:[UITabBarController class]]) {
//        UITabBarController *tabbar = (UITabBarController*)viewVC;
//        naVC = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
//        if (naVC.presentedViewController) {
//            while (naVC.presentedViewController) {
//                naVC = (UINavigationController*)naVC.presentedViewController;
//            }
//        }
//    } else {
//        if ([viewVC isKindOfClass:[UINavigationController class]]) {
//
//            naVC = (UINavigationController*)viewVC;
//            if (naVC.presentedViewController) {
//                while (naVC.presentedViewController) {
//                    naVC = (UINavigationController*)naVC.presentedViewController;
//                }
//            }
//        } else {
//            if ([viewVC isKindOfClass:[UIViewController class]]) {
//                if (viewVC.navigationController) {
//                    return viewVC.navigationController;
//                }
//                return (UINavigationController*)viewVC;
//            }
//        }
//    }
//    return naVC;
//}
//
//+ (UIViewController*)getCurrentUIVC {
//
//    UIViewController *cc;
//    UINavigationController *na = (UINavigationController*)[[self class] getCurrentNaVC];
//
//    if ([na isKindOfClass:[UINavigationController class]]) {
//
//        cc =  na.viewControllers.lastObject;
//        if (cc.childViewControllers.count > 0) {
//            cc = [[self class] getSubUIVCWithVC:cc];
//        }
//    } else {
//        cc = (UIViewController*)na;
//    }
//    return cc;
//}
//
//+ (UIViewController *)getSubUIVCWithVC:(UIViewController*)vc {
//
//    UIViewController *cc;
//    cc = vc.childViewControllers.lastObject;
//    if (cc.childViewControllers > 0) {
//        [[self class] getSubUIVCWithVC:cc];
//    } else {
//        return cc;
//    }
//    return cc;
//}

@end
